//
//  CustomSearch.m
//  小汽车
//
//  Created by 邹壮壮29 on 15-1-4.
//  Copyright (c) 2015年 蓝欧科技. All rights reserved.
//

#import "CustomSearchView.h"
typedef NS_ENUM(NSUInteger, CustomSearchState)
{
    //搜索框状态为隐藏的
    CustomSearchStateNormal = 0,
    //搜索框状态为可见的
    CustomSearchStateSearchBarVisible,
    //搜索框有内容的时候
    CustomSearchStateSearchBarHasContent,
    //搜索框的过度
    CustomSearchStateTransitioning
};

@interface CustomSearchView ()<UITextFieldDelegate,UIGestureRecognizerDelegate>

@property (nonatomic, assign) CustomSearchState state;
@property (nonatomic, retain) UIView *searchFrame;
@property (nonatomic, retain) UITextField *searchField;
@property (nonatomic, retain) UIImageView *searchImageViewOff;
@property (nonatomic, retain) UIImageView *searchImageViewOn;
@property (nonatomic, retain) UIImageView *searchImageCircle;
@property (nonatomic, assign) CGRect	originalFrame;
@end


static CGFloat const CustomSearchBarInset = 11.0;
static CGFloat const CustomSearchBarImageSize = 22.0;


@implementation CustomSearchView
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.searchFrame = [[UIView alloc]initWithFrame:self.bounds];
        self.searchFrame.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
        self.searchFrame.layer.cornerRadius = CGRectGetHeight(self.bounds)/2;
        [self addSubview:self.searchFrame];
        self.searchField = [[UITextField alloc] initWithFrame:CGRectMake(CustomSearchBarInset+CustomSearchBarImageSize, 3.0, CGRectGetWidth(self.bounds) - (2 * CustomSearchBarInset) - CustomSearchBarImageSize *2, CGRectGetHeight(self.bounds) - 6.0)];
        
        self.searchField.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        self.searchField.font = [UIFont systemFontOfSize:16.0];
        self.searchField.textColor = [UIColor colorWithRed:17.0/255.0 green:190.0/255.0 blue:227.0/255.0 alpha:1.0];
        self.searchField.delegate = self;
        [self.searchFrame addSubview:self.searchField];
        
        UIView *searchImageViewOnContainerView = [[UIView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.bounds) - CustomSearchBarInset - CustomSearchBarImageSize, (CGRectGetHeight(self.bounds) - CustomSearchBarImageSize) / 2, CustomSearchBarImageSize, CustomSearchBarImageSize)];
        searchImageViewOnContainerView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
        [self.searchFrame addSubview:searchImageViewOnContainerView];
        
        
        self.searchImageViewOn = [[UIImageView alloc]initWithFrame:searchImageViewOnContainerView.bounds];
        self.searchImageViewOn.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
        self.searchImageViewOn.alpha = 0.0;
        self.searchImageViewOn.image = [UIImage imageNamed:@"NavBarIconSearch_blue"];
        [searchImageViewOnContainerView addSubview:self.searchImageViewOn];
        
        self.searchImageCircle = [[UIImageView alloc] initWithFrame:CGRectMake(3.0, 0.0, 20.0, 20.0)];
        self.searchImageCircle.alpha = 0.0;
        UIImage *myImage = [UIImage  imageNamed:@"iconfont-quxiao-4"];
        UIImage *newImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [self.searchImageCircle setImage:newImage];
        [searchImageViewOnContainerView addSubview:self.searchImageCircle];
        
        self.searchImageViewOff = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.bounds) - CustomSearchBarInset - CustomSearchBarImageSize, (CGRectGetHeight(self.bounds) - CustomSearchBarImageSize) / 2, CustomSearchBarImageSize, CustomSearchBarImageSize)];
        self.searchImageViewOff.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
        self.searchImageViewOff.alpha = 1.0;
        self.searchImageViewOff.image = [UIImage imageNamed:@"NavBarIconSearch_white"];
        [self.searchFrame addSubview:self.searchImageViewOff];
        
        [searchImageViewOnContainerView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(changeStateIfPossible:)]];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:) name:UITextFieldTextDidChangeNotification object:self.searchField];
    }
    return self;
}
- (void)changeStateIfPossible:(UITapGestureRecognizer *)gestureRecognizer{
    switch (self.state) {
        case CustomSearchStateNormal:
        {
            [self showSearchBar:gestureRecognizer];
        }
            break;
        case CustomSearchStateSearchBarVisible:
        {
            [self hideSearchBar:gestureRecognizer];
        }
            break;
        case CustomSearchStateSearchBarHasContent:
        {
            self.searchField.text = nil;
            [self textDidChange:nil];
        }
            break;
        case CustomSearchStateTransitioning:
        {
            
        }
            break;
        default:
            break;
    }
}
- (void)showSearchBar:(id)sender{
    if (self.state == CustomSearchStateNormal) {
        self.state = CustomSearchStateTransitioning;
        self.searchField.text = nil;
        [UIView animateWithDuration:0.25 animations:^{
            self.searchFrame.layer.borderColor = [UIColor whiteColor].CGColor;
            if ([self.delegate respondsToSelector:@selector(destinationFrameForSearchBar:)])
            {
                self.originalFrame = self.frame;
                
                self.frame = [self.delegate destinationFrameForSearchBar:self];
            }
        }completion:^(BOOL finished) {
            [self.searchField becomeFirstResponder];
            [UIView animateWithDuration:0.5 animations:^{
                
                self.searchFrame.layer.backgroundColor = [UIColor whiteColor].CGColor;
                self.searchImageViewOff.alpha = 0.0;
                self.searchImageViewOn.alpha = 1.0;
                self.searchField.alpha = 1.0;
            }completion:^(BOOL finished) {
                self.state = CustomSearchStateSearchBarVisible;
            }];
        }];
    }
}

- (void)hideSearchBar:(id)sender{
    if (self.state == CustomSearchStateSearchBarVisible || self.state == CustomSearchStateSearchBarHasContent) {
        [self.window endEditing:YES];
        self.searchField.text = nil;
        self.state = CustomSearchStateTransitioning;
        [UIView animateWithDuration:0.25 animations:^{
            if ([self.delegate respondsToSelector:@selector(destinationFrameForSearchBar:)])
            {
                self.frame = self.originalFrame;
            }
            self.searchFrame.layer.backgroundColor = [UIColor clearColor].CGColor;
            self.searchImageViewOff.alpha = 1.0;
            self.searchImageViewOn.alpha = 0.0;
            self.searchField.alpha = 0.0;
        }completion:^(BOOL finished) {
            [UIView animateWithDuration:0.25 animations:^{
                self.searchFrame.layer.borderColor = [UIColor clearColor].CGColor;
            }completion:^(BOOL finished) {
                self.searchImageCircle.frame = CGRectMake(3.0, 0.0, 20.0, 20.0);
                self.searchImageCircle.alpha = 0.0;
                self.state = CustomSearchStateNormal;
            }];
        }];
    }
}

- (void)textDidChange:(NSNotification *)notification
{
    BOOL hasText = self.searchField.text.length != 0;
    
    if (hasText)
    {
        if (self.state == CustomSearchStateSearchBarVisible)
        {
            self.state = CustomSearchStateTransitioning;
            self.searchImageViewOn.alpha = 0.0;
            self.searchImageCircle.alpha = 1.0;
            [UIView animateWithDuration:0.25 animations:^{
                
                self.searchImageCircle.frame = CGRectMake(3.0, 0.0, 20.0, 20.0);
            } completion:^(BOOL finished) {
                self.state = CustomSearchStateSearchBarHasContent;
            }];
        }
    }
    else
    {
        if (self.state == CustomSearchStateSearchBarHasContent)
        {
            self.state = CustomSearchStateTransitioning;
            
            [UIView animateWithDuration:0.25 animations:^{
                
                self.searchImageCircle.frame = CGRectMake(3.0, 0.0, 20.0, 20.0);
            } completion:^(BOOL finished) {
                
                self.searchImageViewOn.alpha = 1.0;
                self.searchImageCircle.alpha = 0.0;
                self.state = CustomSearchStateSearchBarVisible;
                
            }];
        }
    }
    
}


@end
